<?php

    namespace app\index\controller;

    use GatewayClient\Gateway;
    use yaliSDK\common\Result;
    use yaliSDK\netstar\NetstarService;
    use yaliSDK\utils\Json;

    class Broadcast {
        static $FLAG='group';
        // *********广播组相关
        //1.添加到广播组，标记id是直播房间号roomId【qqIM创建】
        function joinLiveGroup() {
            $inputJSON = file_get_contents('php://input');
            $params = json_decode($inputJSON, TRUE) ?? [];
            $roomId = $params['roomId'] ?? null;
            $clientId = $params['clientId'] ?? null;// workman的client_id，必须存在
            $service = new NetstarService();
            return Json::classToArray($service->joinLiveGroup($clientId, $roomId));
        }

        function leaveLiveGroup() {
            $inputJSON = file_get_contents('php://input');
            $params = json_decode($inputJSON, TRUE) ?? [];
            $roomId = $params['roomId'] ?? null;
            $clientId = $params['clientId'] ?? null;// workman的client_id，必须存在
            $service = new NetstarService();
            return Json::classToArray($service->leaveLiveGroup($clientId, $roomId));
        }

        // 查找产品所属的直播活动列表的房间号，遍历广播
        function boadcastLiveProduct() {
            $inputJSON = file_get_contents('php://input');
            $params = json_decode($inputJSON, TRUE) ?? [];
            $service = new NetstarService();
            return Json::classToArray($service->broadcastLiveGroup($params));
        }

        function getAllClientSessions() {
            return Json::classToArray([
                'getAllClientCount' => Gateway::getAllClientCount(),
                'getAllClientInfo' => Gateway::getAllClientInfo(),
                'g'=>Gateway::getAllGroupIdList(),

            ]);
        }

        // 加入组
        function joinGroup() {
            $inputJSON = file_get_contents('php://input');
            $params = json_decode($inputJSON, TRUE) ?? [];
            $group = $params['group'] ?? null;//group, array or string
            $clientId = $params['clientId'] ?? null;//client
            if (!empty($clientId)) {

                if (!empty($group)) {
                    if (is_array($group)) {
                        foreach ($group as $item) {

                            Gateway::joinGroup($clientId, self::$FLAG.$item);
                        }
                    } else {
                        Gateway::joinGroup($clientId, self::$FLAG.$group);
                    }
                }
            }
            return Json::classToArray(new Result());
        }

        // 离开组
        function leaveGroup() {
            $inputJSON = file_get_contents('php://input');
            $params = json_decode($inputJSON, TRUE) ?? [];
            $group = $params['group'] ?? null;//group, array or string
            $clientId = $params['clientId'] ?? null;//client
            if (!empty($clientId)) {
                if (!empty($group)) {
                    if (is_array($group)) {
                        foreach ($group as $item) {
                            Gateway::leaveGroup($clientId, self::$FLAG.$item);
                        }
                    } else {
                        Gateway::leaveGroup($clientId, self::$FLAG.$group);
                    }
                }
            }
            return Json::classToArray(new Result());
        }

        // 通知
        function sendGroup() {
            $inputJSON = file_get_contents('php://input');
            $params = json_decode($inputJSON, TRUE) ?? [];
            $msg = $params['msg'] ?? '';
            $group = $params['group'] ?? null;//group, array or string
            if (is_array($msg)) {
                $msg = json_encode($msg)?? '';
            }
            try {
                if (!empty($group) && !empty($msg)) {

                    if (is_array($group)) {
                        foreach ($group as $item) {

                            Gateway::sendToGroup(self::$FLAG.$item, $msg);
                        }
                    } else {
                        Gateway::sendToGroup(self::$FLAG.$group, $msg);
                    }
                }
                return 1;
            } catch (\Exception $e) {
                return 0;
            }

        }

    }